package com.zoshine.zzg.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.zoshine.zzg.annotation.DisableLoginCheck;
import com.zoshine.zzg.base.BaseResponse;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.RelationSupplierPrison;
import com.zoshine.zzg.pojo.pojostatus.RelationSupplierPrisonStatus;
import com.zoshine.zzg.respons.AppResponse;
import com.zoshine.zzg.service.RelationSupplierPrisonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 田沛凌
 * @time 2017/8/16 9:31
 */
@RestController
@RequestMapping(value = "relationSupplierPrison", method = RequestMethod.POST)
public class RelationSupplierPrisonController extends BaseController {
    private RelationSupplierPrisonService relationSupplierPrisonService;

    @Autowired
    public void setRelationSupplierPrisonerService(RelationSupplierPrisonService relationSupplierPrisonService) {
        this.relationSupplierPrisonService = relationSupplierPrisonService;
    }

    /**
     * 新增供应商与监所关系表
     *
     * @param relationSupplierPrison；
     */
    @RequestMapping("addRelationSupplierPrison")
    public BaseResponse addRelationSupplierPrison(RelationSupplierPrison relationSupplierPrison) {
        //如果存在就不再继续增加
        List<RelationSupplierPrison> list = relationSupplierPrisonService.list(relationSupplierPrison);
        if (list.size() > 0) {
            throw new AppException(ErrorMessage.RELATION_HAS_BEN_EXIT);
        }

        relationSupplierPrison.setStatus(RelationSupplierPrisonStatus.TOBE_AUDITED.getValue());
        relationSupplierPrisonService.add(relationSupplierPrison);
        return AppResponse.OK;
    }

    /**
     * 查询供应商与监所关系表
     *
     * @param relationSupplierPrison；
     * @return 集合
     */
    @DisableLoginCheck
    @RequestMapping("/pageRelationSupplierPrison")
    public BaseResponse pageRelationSupplierPrison(RelationSupplierPrison relationSupplierPrison) {
        List<RelationSupplierPrison> list = relationSupplierPrisonService.list(relationSupplierPrison);
        return new AppResponse(new PageInfo<>(list));
    }

    /**
     * 删除供应商与监所关系表
     *
     * @param id 关系ID
     */
    @RequestMapping("/delRelationSupplierPrison")
    public BaseResponse delRelationSupplierPrison(Long id) {
        if (id == null) {
            throw new AppException(ErrorMessage.REQUEST_PARAM_CAN_NOT_NULL);
        }
        relationSupplierPrisonService.del(id);
        return AppResponse.OK;
    }

    /**
     * 修改供应商与监所关系表
     *
     * @param relationSupplierPrison；
     */
    @RequestMapping("editRelationSupplierPrison")
    public BaseResponse editRelationSupplierPrison(RelationSupplierPrison relationSupplierPrison) {
        relationSupplierPrisonService.edit(relationSupplierPrison);
        return AppResponse.OK;
    }

    /**
     * 审核通过
     *
     * @param relationSupplierPrison；
     */
    @RequestMapping("auditRelationSupplierPrison")
    public BaseResponse auditRelationSupplierPrison(String relationSupplierPrison) {
        RelationSupplierPrison temp = JSON.parseObject(relationSupplierPrison, RelationSupplierPrison.class);
        if (null == temp.getSupplierId()) {
            throw new AppException(ErrorMessage.ACCOUNT_SUPPLIER_ID_CAN_NOT_NULL);
        }
        if (null == temp.getPrisonId()) {
            throw new AppException(ErrorMessage.ACCOUNT_PRISONS_ID_CAN_NOT_NULL);
        }
        RelationSupplierPrison verification  = relationSupplierPrisonService.get(temp);
        if (!(verification.getStatus().equals(RelationSupplierPrisonStatus.TOBE_AUDITED.getValue()))){
            throw new AppException(ErrorMessage.RELATION_HAS_BEN_AUDIT);
        }
        temp.setStatus(RelationSupplierPrisonStatus.AUDITED.getValue());
        relationSupplierPrisonService.edit(temp);
        return AppResponse.OK;
    }

    /**
     * 审核不通过
     *
     * @param relationSupplierPrison；
     */
    @RequestMapping("failRelationSupplierPrison")
    public BaseResponse failRelationSupplierPrison(String relationSupplierPrison) {
        RelationSupplierPrison temp = JSON.parseObject(relationSupplierPrison, RelationSupplierPrison.class);
        if (null == temp.getSupplierId()) {
            throw new AppException(ErrorMessage.ACCOUNT_SUPPLIER_ID_CAN_NOT_NULL);
        }
        if (null == temp.getPrisonId()) {
            throw new AppException(ErrorMessage.ACCOUNT_PRISONS_ID_CAN_NOT_NULL);
        }
        RelationSupplierPrison verification  = relationSupplierPrisonService.get(temp);
        if (!(verification.getStatus().equals(RelationSupplierPrisonStatus.TOBE_AUDITED.getValue()))){
            throw new AppException(ErrorMessage.RELATION_HAS_BEN_AUDIT);
        }
        temp.setStatus(RelationSupplierPrisonStatus.AUDITED_FAIL.getValue());
        relationSupplierPrisonService.edit(temp);
        return AppResponse.OK;
    }

    /**
     * 初始化
     *
     * @param relationSupplierPrison；
     */
    @RequestMapping("initRelationSupplierPrison")
    public BaseResponse initRelationSupplierPrison(String relationSupplierPrison) {
        RelationSupplierPrison temp = JSON.parseObject(relationSupplierPrison, RelationSupplierPrison.class);
        if (null == temp.getSupplierId()) {
            throw new AppException(ErrorMessage.ACCOUNT_SUPPLIER_ID_CAN_NOT_NULL);
        }
        if (null == temp.getPrisonId()) {
            throw new AppException(ErrorMessage.ACCOUNT_PRISONS_ID_CAN_NOT_NULL);
        }
        RelationSupplierPrison verification  = relationSupplierPrisonService.get(temp);
        if (verification.getStatus().equals(RelationSupplierPrisonStatus.TOBE_AUDITED.getValue())){
            throw new AppException(ErrorMessage.RELATION_STATUS_BEN_AUDIT);
        }
        temp.setStatus(RelationSupplierPrisonStatus.TOBE_AUDITED.getValue());
        relationSupplierPrisonService.edit(temp);
        return AppResponse.OK;
    }

}
